package org.geo.common.oss;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectResult;

import java.io.InputStream;

/**
 * 阿里云OSS客户端
 *
 * @author ljy
 */
public class OssClientHelper {

    /**
     * 上传头像文件
     *
     * @param inputStream InputStream
     * @param fileSize    文件大小
     * @param contentType 文件类型
     * @param fileName    文件名
     * @return 文件URL
     */
    public static String uploadAvatarFile(InputStream inputStream, long fileSize, String contentType, String fileName) {
        if (inputStream != null) {
            ObjectMetadata metadata = new ObjectMetadata();
            metadata.setContentType(contentType);
            metadata.setContentLength(fileSize);
            OSSClient ossClient = new OSSClient(OssConstants.ENDPOINT_OUTER, OssConstants.ACCESS_ID_KEY, OssConstants.ACCESS_ID_SECRET);
            PutObjectResult putObjectResult = ossClient.putObject(OssConstants.BUCKET_AVATAR, fileName, inputStream, metadata);
            String eTag = putObjectResult.getETag();
            System.out.println(eTag);
            ossClient.shutdown();
            return OssConstants.HTTP + OssConstants.BUCKET_AVATAR + "." + OssConstants.ENDPOINT_OUTER + "/" + fileName;
        }
        return "";
    }

}
